// pages/msbd/msbd.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    list: [],
    currentPage: 0,
    Pagesize: 20,
    total: 0,
    IscurrentPage: false, //判断是否还有数据
    SkeletonScreen: true, //骨架屏
    Isshow: false, //判断用户信息
    Collection: [], //已经收藏的数据
    listCollection: [], //改变状态的数据


  },
  getlist(cback) { //渲染
    wx.request({
      url: 'http://localhost:3001/msbd/get-msbd', //仅为示例，并非真实的接口地址
      data: {
        currentPage: this.data.currentPage
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        for (let i = 0; i < res.data.data.data.length; i++) {
          res.data.data.data[i].IsCollection = false //添加唯一id后续根据index判断
        }
        this.setData({
          list: [...this.data.list, ...res.data.data.data],
          total: res.data.total,
        })
        let IsPage = Math.ceil(this.data.total / this.data.Pagesize)
        console.log('当前页' + this.data.currentPage, '总页数' + IsPage);
        this.setData({
          SkeletonScreen: false, //骨架屏
          IscurrentPage: IsPage - 1 == this.data.currentPage
        })
        cback && cback()
      }
    })
  },

  Binduser() { //跳转登陆页面
    wx.switchTab({
      url: '../settings/settings',
    })
  },
  onClose(event) {
    const {
      instance
    } = event.detail;
    Dialog.confirm({
      message: '确定收藏吗？',
    }).then(() => {
      this.data.list[event.target.dataset.list[1]].IsCollection = true;
      this.data.Collection.push({
        list: event.target.dataset.list,
      })
      this.setData({
        list: this.data.list,
        Collection: this.data.Collection
      })
      let arr = {};
      arr[event.target.dataset.list[1]](this.data.list[event.target.dataset.list[1]].id)
      wx.setStorageSync(
        'Val', arr,
      )
      app.globalData.MsbdListCollection = this.data.Collection;
      Toast.success('已收藏');
      instance.close();
    }).catch(() => {
      instance.close();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlist()
    console.log(this.data.list);
    console.log(
      this.setData({
        listCollection: wx.getStorageSync('Val')
      })

    );

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.bkUserList);
    this.setData({
      Isshow: app.globalData.bkUserList ? true : false,
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('下拉刷新事件', this.data.currentPage)
    this.setData({
      list: [],
      currentPage: 0,
      IscurrentPage: false
    })
    wx.showLoading({
      title: '加载中',

    })
    this.getlist(() => {
      wx.hideLoading() //关闭loading
      wx.stopPullDownRefresh() //停止当前页面下拉刷新
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('上拉刷新事件', this.data.currentPage)
    this.setData({
      currentPage: this.data.currentPage + 1
    })
    if (this.data.IscurrentPage) return
    this.getlist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})